package com.ylzinfo.ms.utils.RSA;


import org.apache.tomcat.util.codec.binary.Base64;
import org.apache.tomcat.util.codec.binary.StringUtils;

public class CryptoUtils {


    /**
     * 公钥加密转base64输出
     *
     * @param str
     * @param puk 公钥
     * @return
     * @throws Exception
     */
    public static String encryptStrByPuk(String str, String puk) throws Exception {
        byte[] encrypted = RSACrypt.encryptByPublicKey(str.getBytes(), Base64.decodeBase64(puk));
        return Base64.encodeBase64String(encrypted);
    }

    /**
     * 私钥解密转string
     *
     * @param str
     * @param pri 私钥
     * @return
     * @throws Exception
     */
    public static String descryptStrByPri(String str, String pri) throws Exception {
        byte[] encrypted = RSACrypt.decryptByPrivateKey(Base64.decodeBase64(str), Base64.decodeBase64(pri));
        return StringUtils.newStringUtf8(encrypted);
    }


    /**
     * 私钥加密转base64
     *
     * @param str
     * @param pri
     * @return
     * @throws Exception
     */
    public static String encryptStrByPri(String str, String pri) throws Exception {
        byte[] encrypted = RSACrypt.encryptByPrivateKey(str.getBytes(), Base64.decodeBase64(pri));
        return Base64.encodeBase64String(encrypted);
    }


    /**
     * 公钥解密转String
     *
     * @param str
     * @param puk 公钥
     * @return
     * @throws Exception
     */
    public static String decryptStrByPuk(String str, String puk) throws Exception {
        byte[] encrypted = RSACrypt.decryptByPublicKey(Base64.decodeBase64(str), Base64.decodeBase64(puk));
        return StringUtils.newStringUtf8(encrypted);
    }

}
